using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using KingSite.Shop.Core.Service;
using KingSite.Shop.Service;
using KingSite.Shop.Domain;
using KingSite.Shop.Core.Mvc;

namespace KingSite.Shop.Web.Controllers
{
    public class CategoryController : BaseController
    {
        ICategoryService _service;
        IParserUrlParamater pup = new ParserUrlParamater();

        public CategoryController(ICategoryService service) {
            _service = service;
        }
        //
        // GET: /Category/

        public ActionResult Index()
        {
            IList<Category> list = _service.FindAll();
            ViewData.Model = list;

            //var rootList = from a in list where a.ParentId.Equals(0) select a;
            //foreach (var r in rootList) {            
            //    var fList = from a in list where a.ParentId.Equals(r.Id) select a;
            //    foreach (var f in fList) {
            //        var sList = from a in list where a.ParentId.Equals(f.Id) select a;
            //        foreach (var s in sList) {                         
            //        }
            //    }
            //}
            return View();
        }

        #region UserControl

        public ActionResult All() {
            IList<Category> list = _service.FindAll();
            for (int i = 0; i <= list.Count - 1; i++) {
                list[i].Path = pup.Compile(list[i].Path);
            }
            ViewData.Model = list;
            return View();
        }

        public ActionResult SubCategory(int id) {
            Category category = _service.Find(id);
            if (category != null) {
                ViewData["CategoryId"] = category.Id;
                ViewData["CategoryName"] = category.Name;
            }
            IList<Category> list = _service.FindAll();
            for (int i = 0; i <= list.Count - 1; i++) {
                list[i].Path = pup.Compile(list[i].Path);
            }
            ViewData.Model = list;
            return View();
        }

        public ActionResult TopCategory() {
            IList<Category> list = _service.FindAll();
            for (int i = 0; i <= list.Count - 1; i++) {
                list[i].Path = pup.Compile(list[i].Path);
            }
            ViewData.Model = list;
            return View();

        }

        #endregion
    }
}
